﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SystemCenterSoa.ApiLibrary.PermissionsValidation;
using SystemCenterSoa.BaseLibrary.Tools.TypeTools;
using SystemCenterSoa.DtoModel.Common;
using SystemCenterSoa.DtoModel.SystemManager;
using SystemCenterSoa.PoModel.SystemManager;
using SystemCenterSoa.Service.SystemManager;

namespace SystemCenterSoa.ApiProject.Area.SystemManager
{
    [ApiModuleRelation("module_manager")]
    public class ModuleManagerController : SystemManagerBaseController
    {
        private ModuleService _moduleService;
        public ModuleManagerController(ModuleService moduleService)
        {
            _moduleService = moduleService;
        }
        [HttpGet]
        public ResultModel<List<ModulePo>> GetModulePosAll()
        {
            try
            {
                var data = _moduleService.GetModulePosAll();
                return MyOk(data);
            }
            catch (Exception ex)
            {
                return MyError<List<ModulePo>>(ex);
            }
        }
        [HttpGet]
        public ResultModel<List<ModuleDto>> GetModuleDtosAll()
        {
            try
            {
                var data = _moduleService.GetModuleDtosAll();
                return MyOk(data);
            }
            catch (Exception ex)
            {
                return MyError<List<ModuleDto>>(ex);
            }
        }
        [HttpGet]
        public ResultModel<bool> VModuleCode(string mc)
        {
            try
            {
                var data = _moduleService.VModuleCode(mc);
                return MyOk<bool>(data);
            }
            catch (Exception ex)
            {
                return MyError<bool>(ex);
            }
        }
        [HttpPost]
        public ResultModel<bool> ModuleAdd(ModuleDto view)
        {
            if (string.IsNullOrWhiteSpace(view.module_code))
            {
                return MyFail<bool>("请输入模块code");
            }
            if (!_moduleService.VModuleCode(view.module_code))
            {
                return MyFail<bool>("模块code已经被使用,请重新输入其他模块Code");
            }

            ModulePo dbModel = ModelCopier.CreateInstanceFrom<ModulePo>(view);
            dbModel.creator_date = DateTime.Now;
            dbModel.creator_name = LoginUser.user_name;
            try
            {
                var data = _moduleService.ModuleAdd(dbModel);
                return MyOk<bool>(data);
            }
            catch (Exception ex)
            {
                return MyError<bool>(ex);
            }
        }

        [HttpPost]
        public ResultModel<bool> ModuleUpdate(ModuleDto view)
        {
            if (string.IsNullOrWhiteSpace(view.module_code))
            {
                return MyFail<bool>("参数错误, 缺少module_code参数");
            }
            ModulePo dbModel = ModelCopier.CreateInstanceFrom<ModulePo>(view);
            dbModel.modifi_date = DateTime.Now;
            dbModel.modifi_name = LoginUser.user_name;

            try
            {
                var data = _moduleService.ModuleUpdate(dbModel);
                return MyOk(data);
            }
            catch (Exception ex)
            {
                return MyError<bool>(ex);
            }
        }

        [HttpGet]
        public ResultModel<ModuleDto> GetModuleDtoByCode(string code)
        {
            if (string.IsNullOrWhiteSpace(code))
            {
                return MyFail<ModuleDto>("参数错误, 缺少code参数");
            }
            try
            {
                var data = _moduleService.GetModuleDtoByCode(code);
                return MyOk(data);
            }
            catch (Exception ex)
            {
                return MyError<ModuleDto>(ex);
            }

        }
        [HttpGet]
        public ResultModel<ModuleDto> GetModuleDtoByCodeSonSql(string moduleCode)
        {
            try
            {
                var data = _moduleService.GetModuleDtoByCodeSonSql(moduleCode);
                return MyOk(data);
            }
            catch (Exception ex)
            {
                return MyError<ModuleDto>(ex);
            }
        }

    }
}
